package com.mr.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mr.model.BaseInfo;
import com.mr.model.Member;
import com.mr.model.Plan;
import com.mr.model.SearchCommand;
import com.mr.model.Shop;
import com.mr.model.Status;
import com.mr.util.DesUtils;
import com.mr.util.RandomUtil;
import com.mr.util.StringUtil;
import com.mr.util.ValidateUtil;
import com.mr.web.support.PageUtil;

@Controller
public class ProxyController extends AnnotationController{
	
	@RequestMapping("/auth/member/proxy/shopForm.xhtml")
	public String proxyForm(Long id, ModelMap model) {
		if(id!=null) {
			Shop shop = daoService.getObject(Shop.class, id);
			model.put("shop", shop);
		}
		model.put("isMod", true);
		return "shop/shopForm.vm";
	}
	@RequestMapping("/auth/member/proxy/shopDetail.xhtml")
	public String shopDetail(Long id, ModelMap model) {
		if(id!=null) {
			Shop shop = daoService.getObject(Shop.class, id);
			model.put("shop", shop);
		}
		return "shop/shopForm.vm";
	}
	@RequestMapping("/auth/member/proxy/saveShop.xhtml")
	public String saveProxy(Long id, HttpServletRequest req, ModelMap model) {
		BaseInfo base = (BaseInfo)req.getSession().getAttribute("base");
		Long proxyid = base.getId();
		Shop shop = new Shop();
		if(id!=null) {
			shop = daoService.getObject(Shop.class, id);
		}else {
			String membername = RandomUtil.getRandChar(6);
			String p = RandomUtil.getRandNum(8);
			String password = StringUtil.md5(p);
			String pwd = DesUtils.encrypt(p);
			Member member = new Member(membername, password, pwd, Member.MEMBERTYPE_SHOP, base);
			shop.setProxyid(proxyid);
			daoService.saveObject(member);
			id = member.getId();
		}
		try {
			bindData(shop, req.getParameterMap());
			shop.setId(id);
			String msg = ValidateUtil.validate(shop, ",");
			if(StringUtils.isNotBlank(msg)) return showJsonFailure(msg, model);
			daoService.saveObject(shop);
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.put("id", shop.getId());
		return "redirect:/auth/member/proxy/shopList.xhtml";
	}
	@RequestMapping("/auth/member/proxy/shopList.xhtml")
	public String userList(SearchCommand command, Integer pageNo, HttpServletRequest req, ModelMap model){
		BaseInfo base = (BaseInfo)req.getSession().getAttribute("base");
		Long proxyid = base.getId();
		if(pageNo==null) pageNo = 0;
		Integer limit = 15;
		Integer count = commonService.getShopCount(proxyid, command, Status.Y);
		List<Shop> shopList = commonService.getShopList(proxyid, command, Status.Y, pageNo*limit, limit);
		
		Integer rowsPerPage = limit;
		PageUtil pageUtil = new PageUtil(count, rowsPerPage, pageNo, "auth/member/proxy/shopList.xhtml");
		Map params = new HashMap();
		pageUtil.initPageInfo(params);
		model.put("pageUtil", pageUtil);
		
		model.put("shopList", shopList);
		model.put("count", count);
		return "shop/shopList.vm";
	}
	@RequestMapping("/auth/member/proxy/updateShop.xhtml")
	public String updateShop(String enId, ModelMap model) {
		Long id = Long.valueOf(DesUtils.decrypt(enId));
		Shop shop = daoService.getObject(Shop.class, id);
		shop.setStatus(Status.D);
		daoService.saveObject(shop);
		return showJsonSuccess(model);
	}
	
	@RequestMapping("/auth/member/proxy/planList.xhtml")
	public String planList(String name, Integer pageNo, HttpServletRequest req, ModelMap model){
		BaseInfo base = (BaseInfo)req.getSession().getAttribute("base");
		Long relatedid = base.getId();
		if(pageNo==null) pageNo = 0;
		Integer limit = 15;
		Integer count = commonService.getPlanCount(relatedid, name, Status.Y);
		List<Plan> planList = commonService.getPlanList(relatedid, name, Status.Y, pageNo*limit, limit);
		
		Integer rowsPerPage = limit;
		PageUtil pageUtil = new PageUtil(count, rowsPerPage, pageNo, "auth/member/proxy/planList.xhtml");
		Map params = new HashMap();
		pageUtil.initPageInfo(params);
		model.put("pageUtil", pageUtil);
		
		model.put("count", count);
		model.put("planList", planList);
		return "proxy/planList.vm";
	}
	@RequestMapping("/auth/member/proxy/planForm.xhtml")
	public String planForm(Long id, HttpServletRequest req, ModelMap model){
		if(id!=null) {
			Plan plan = daoService.getObject(Plan.class, id);
			model.put("plan", plan);
		}
		return "proxy/planForm.vm";
	}
	
	@RequestMapping("/auth/member/proxy/savePlan.xhtml")
	public String savePlan(Long id, HttpServletRequest req, ModelMap model){
		BaseInfo base = (BaseInfo)req.getSession().getAttribute("base");
		Long relatedid = base.getId();
		Plan plan = new Plan();
		if(id!=null) {
			plan = daoService.getObject(Plan.class, id);
		}else {
			plan.setRelatedid(relatedid);
		}
		try {
			bindData(plan, req.getParameterMap());
			String msg = ValidateUtil.validate(plan, ",");
			if(StringUtils.isNotBlank(msg)) return showJsonFailure(msg, model);
			daoService.saveObject(plan);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/auth/member/proxy/planList.xhtml";
	}
	@RequestMapping("/auth/member/proxy/updatePlan.xhtml")
	public String updatePlan(String enId, ModelMap model){
		try {
			Long id = Long.valueOf(DesUtils.decrypt(enId));
			Plan plan = daoService.getObject(Plan.class, id);
			plan.setStatus(Status.D);
			daoService.saveObject(plan);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return showJsonFailure("删除失败！", model);
		}
		return showJsonSuccess(model);
	}
	
}
